package edu.cuit.jead.mynetdisk.service;

import edu.cuit.jead.mynetdisk.dao.UserMapper;
import edu.cuit.jead.mynetdisk.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.math.BigInteger;

@Service
public class UserService {
    @Autowired
    UserMapper userMapper;
    User user=new User();
    public User login(String useremail, String password,String token) {
        System.out.println("useremail="+useremail);
        userMapper.updateToken(token,useremail);
        User result = new User();
        result = userMapper.getUserByEmail(useremail);
        return result;
    }
    //注册
    public Integer register(User newuser){
        try{
            User finduser=new User();
            finduser = userMapper.getUserByEmail(newuser.getEmail());
            if(finduser!=null){
                return 1;//email存在
            }
            finduser=userMapper.getUserByName(newuser.getUsername());
            if(finduser!=null){
                return 2;//用户名存在
            }
            User user=new User();
            user.setEmail(newuser.getEmail());
            user.setPassword(newuser.getPassword());
            user.setUsername(newuser.getUsername());
            user.setAvatar("E:\\java\\java_project\\files\\pictures\\default.jpg");
            Long space = 10737418240L;
            user.setTotalspace(space);
            user.setUsedspace(0L);
            user.setStatus(1);
            user.setCreatetime(userMapper.getCurrentTime());
            user.setUpdatetime(userMapper.getCurrentTime());
            userMapper.insert(user);
            return 3;//成功
    }
    catch (Exception e){
        e.printStackTrace();
        return null;
        }
    }
    public User userdata(String email){
        User data=new User();
        data=userMapper.getUserByEmail(email);
        System.out.println(data);
        return data;
    }

    public int changePicture(String avatar, String email){
        System.out.println("email="+email);
        return userMapper.updateAvatar(avatar,email);
    }
    public User findUserByName(String name){
        return userMapper.getUserByName(name);
    }
    public Integer updateUsername(String username,String email){
        return userMapper.updateUsername(username,email);
    }
    public Integer updatePassword(String password,String email){
        return userMapper.updatePassword(password,email);
    }
}
